/**
 * Created by chenyong on 2015/11/30.
 */

var request = require('request');
var config = require('./../config');
var bigpixelsrv = require('./bigpixelsrv');
//更新为redis数据缓存方式{2015-12-23 created by wangkui}
var CachemanRedis = require('cacheman-redis');
var cache = new CachemanRedis(config.cachestore);

exports.register = function(socket) {
    socket.on('getusertags', function(panoid, skip,limit,gendar){
        try
        {
            var form = {
                panoid:panoid,
                sex:gendar
            };
            var url = "/api/panos/" + panoid + "/tags?skip="+skip+"&limit="+limit;
            var key='Socket_Tags_'+panoid+'_'+skip+'_'+limit;
            cache.get(key,function(err,value){
                if(err|| value==undefined||value==''){
                    bigpixelsrv.GetData(url,form,function(err,data){
                        if(!err)
                        {
                            cache.set(key,data,90*1000,function(err){
                                if(err){console.log(err);}
                            });
                            socket.emit('getusertags',data)
                        }
                        else
                        {
                            socket.emit('error','读取标注点错误');
                            console.log(err);
                        }
                    })
                } else{
                    socket.emit('getusertags',value);
                }
            });
        }
        catch(e)
        {
            console.log(e);
          socket.emit('error',e);
        }
    });

    socket.on('randomusertags',function(panoid,count){
        try
        {
            var url = '/api/panos/' + panoid + '/randomtags?count=' + count + '&tagtype=UserTag';
            var key='Socket_Randomtags_'+panoid+'_'+count;
            cache.get(key,function(err,value){
                if(err|| value==undefined||value==''){
                    bigpixelsrv.GetData(url,function(err,data){
                        if(!err)
                        {
                            cache.set(key,data,90*1000,function(err){
                                if(err){console.log(err);}
                            });
                            socket.emit('randomusertags',data);
                        }
                        else
                        {
                            console.log(err);
                            socket.emit('error',err);
                        }
                    })
                }
                else{
                    socket.emit('randomusertags',value);
                }
            });
        }
        catch(e)
        {
            console.log(e);
          socket.emit('error',e);
        }
    });

    socket.on('createusertag',function(panoid,userid,ath,atv,fov,note,backgroundpic){
        try
        {
            var url = '/api/panos/'+panoid+'/tags';
            var body = {ath:ath, atv:atv, fov:fov, note:note, userid:userid, tagtype:'UserTag'};
            if(backgroundpic != undefined && backgroundpic != "")
            {
                body.backgroundpic = backgroundpic;
            }
            bigpixelsrv.PostData(url,body,function(error,data){
                if(!error)
                {
                    socket.emit('createusertag-success',data);
                }
                else
                {
                    console.log(error);
                  socket.emit('error',error);
                }
            })
        }
        catch(e)
        {
          socket.emit('error',e);
            console.log(e);
        }
    });
}
